<?php
class Account_model extends MY_Model {
	private $table = 'user';

	function getsearchContent($limit = 10,$page = 0){
		$this->db->select('*');
		$this->db->limit($limit,$page);
		$this->db->order_by('created','DESC');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->where('`email` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function getTotalsearchContent(){
		$this->db->select('*');
		if($this->input->post('content')!='' && $this->input->post('content')!='type here...'){
			$this->db->or_where('`email` LIKE "%'.$this->input->post('content').'%"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')==''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
		}
		if($this->input->post('dateFrom')=='' && $this->input->post('dateTo')!=''){
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		if($this->input->post('dateFrom')!='' && $this->input->post('dateTo')!=''){
			$this->db->where('created >= "'.date('Y-m-d 00:00:00',strtotime($this->input->post('dateFrom'))).'"');
			$this->db->where('created <= "'.date('Y-m-d 23:59:59',strtotime($this->input->post('dateTo'))).'"');
		}
		$query = $this->db->get(PREFIX.$this->table);
		
		if($query->result()){
			return count($query->result());
		}else{
			return false;
		}
	}
	
	function getDetailManagement($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
	
	function saveManagement(){	
		if($this->input->post('status')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}
		
		if($this->input->post('hiddenId')== 0){
			$data = array(
				'fullname'	=> $this->input->post('fullname'),
				'username'	=> $this->input->post('username'),
				'password'	=> md5($this->input->post('password')),
				'email'		=> $this->input->post('email'),
				'address'	=> $this->input->post('address'),
				'phone'		=> $this->input->post('phone'),
				'sex'		=> $this->input->post('sex'),
				'ip'		=> getIP(),
				'status'	=> $status,
				'created'	=> date('Y-m-d H:i:s',time()),
				'modified'	=> date('Y-m-d H:i:s',time())
			);
			if($this->db->insert(PREFIX.$this->table,$data)){
				return true;
			}
		}else{
			$password = $this->input->post('passwordAdmincp');
			if($password == '')
			{
				$result = $this->getPass($this->input->post('hiddenId'));
				$password = $result[0]->password;
			}else{
				$password = md5($this->input->post('password'));
			}
			$data = array(
				'fullname'	=> $this->input->post('fullname'),
				'username'	=> $this->input->post('username'),
				'password'	=> $password,
				'email'		=> $this->input->post('email'),
				'address'	=> $this->input->post('address'),
				'phone'		=> $this->input->post('phone'),
				'sex'		=> $this->input->post('sex'),
				'ip'		=> getIP(),
				'status'	=> $status,
				'modified'	=> date('Y-m-d H:i:s',time())
			);
			$this->db->where('id',$this->input->post('hiddenId'));
			if($this->db->update(PREFIX.$this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	private function getPass($id = 0)
	{
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get(PREFIX.$this->table);
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return $result;
		}
	}
	/*
	*
	*	FRONTEND
	*/
	function saveProfile()
	{
		$user_id = $this->session->userdata('userId');
		if($user_id == '' || !empty($user_id))
		{
			$data = array(
				'fullname'		=> $this->input->post('fullname'),
				'username'		=> $this->input->post('username'),
				'cmnd'			=> $this->input->post('cmnd'),
				'password'		=> md5($this->input->post('password')),
				'email'			=> $this->input->post('email'),
				'address'		=> $this->input->post('address'),
				'phone'			=> $this->input->post('phone'),
				'ip'			=> getIP(),
				'is_active'		=> 1,
				'created'		=> date('Y-m-d H:i:s',time()),
				'modified'		=> date('Y-m-d H:i:s',time())
			);
			
			if($this->db->insert(PREFIX.$this->table,$data))
			{
				return true;
			}
		}else{			
			$data = array();
			$data['fullname']		= $this->input->post('fullname');
			if($this->input->post('password'))
			{
				$data['password']	= $this->input->post('password');
			}
			//$data['province_id']	= $this->input->post('province_id');
			$data['address']		= $this->input->post('address');
			$data['phone']			= $this->input->post('phone');
			$data['modified']		= date('Y-m-d H:i:s',time());
			
			$this->db->where('id',$user_id);
			if($this->db->update(PREFIX.$this->table,$data))
			{
				return true;
			}
		}
		return false;
	}
	
	public function getInformation($username = '')
	{
		$this->db->select('id');
		$this->db->where('username',$username);
		$query = $this->db->get(PREFIX.$this->table);
		$result = $query->result();
		
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	
	/*
	*
	*	Function checkExists()
	*	Des: Kiem tra su ton tai cua user
	*/
	
	function checkExists($input = '',$field = '')
	{
		$this->db->select('id');
		$this->db->where("{$input}",$field);
		$query = $this->db->get(PREFIX.$this->table);
		
		$result = $query->result();
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	
	/*
	*
	*	Function login()
	*	Des: Dang nhap
	*/
	
	function checkLogin($user = '',$password = ''){
		$this->db->select('*');
		$this->db->where('username', $user);
		$this->db->where('password', $password);
		$this->db->where('status',1);
		$query = $this->db->get(PREFIX.$this->table);
		$result = $query->result();
		
		if($result){
			return $result;
		}else{
			return false;
		}
	}
	
	function getProvince()
	{
		$this->db->select('*');
		$this->db->where('is_active',1);
		$this->db->order_by('order DESC,slug','DESC');
		$result = $this->db->get(PREFIX.'district');
		
		if($result)
		{
			return $result;
		}else{
			return false;
		}
	}
	/*
	*
	*	ENDFRONTEND
	*/
	
}